package com.yogee.topseller.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.AllMechanismActivity;
import com.yogee.topseller.activity.SearchByConditionsActivity;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class InterestLabelSxAdapter extends RecyclerView.Adapter{
    //上下文
    Activity mContext;
    private int TYPE_ITEM = 0, TYPE_FOOT = 1;
    private boolean flag = true;
    //标签数据
    List<InterestLabelBean.ListBean> mLabelDatas,allLabelDatas,mSelectDatas;
    //已选中的标签
    public ArrayList<TextView> mSelectView = new ArrayList<>();
    public ArrayList<TextView> mSelectView1 = new ArrayList<>();

    private InterestLabelBean interestLabelBean,jgBean;

    private ArrayList<InterestLabelBean.ListBean.LabelListBean> mSelectBean = new ArrayList<>();

    private List<String> selectStr = new ArrayList<>();
    private int current_position=-1;
    private String qtje="0",syStart="0",syEnd="0";

    public InterestLabelSxAdapter(Activity context) {
        this.mContext = context;
    }

    public void setCity(InterestLabelBean interestLabelBean){

        this.interestLabelBean = interestLabelBean;
    }

    public void setJigou(InterestLabelBean interestLabelBean){
        this.jgBean = interestLabelBean;
    }


    public void addData(List<InterestLabelBean.ListBean> mLabelDatas) {
        this.mLabelDatas = new ArrayList<>();
        this.allLabelDatas = new ArrayList<>();
        this.mLabelDatas = mLabelDatas;
        this.allLabelDatas = mLabelDatas;
        this.mSelectDatas = new ArrayList<>();

        if(allLabelDatas!=null&&allLabelDatas.size()>0){
            for(int i = 0 ;i<allLabelDatas.size();i++){
                InterestLabelBean.ListBean listbean = new InterestLabelBean.ListBean();
                listbean.setType(allLabelDatas.get(i).getType());
                mSelectDatas.add(listbean);
            }
        }
        notifyDataSetChanged();
    }

    public ArrayList<InterestLabelBean.ListBean.LabelListBean> getmSelectBean() {
        return mSelectBean;
    }

    public List<String> getmSelectStr(){
        return selectStr;
    }

    public boolean checkValue(){

        if(Float.valueOf(syEnd)<Float.valueOf(syStart)){
            ToastUtils.showToast(mContext,"收益区间截止数值必须大于起始数值");
            return false;
        }else {
            return true;
        }
    }

    public List<InterestLabelBean.ListBean> getmSelectDatas(){
        if(TextUtils.isEmpty(qtje))
            qtje = "0";
        if(TextUtils.isEmpty(syStart))
            syStart = "0";
        if(TextUtils.isEmpty(syEnd))
            syEnd = "0";
        mSelectDatas.get(1).setType(qtje+","+syStart+","+syEnd);
        return mSelectDatas;

    }
    public void setSelectItem(int position,List<InterestLabelBean.ListBean.LabelListBean> labelListBean){
        mLabelDatas.get(position).setLabelList(labelListBean);
        current_position = position;
        notifyItemChanged(position);

    }

    @Override
    public int getItemViewType(int position) {
        if(flag){
            if (position == 1) {
                return TYPE_FOOT;
            } else {
                if(position == mLabelDatas.size()){
                    flag = false;
                }
                return TYPE_ITEM;
            }
        }
        return TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        if(viewHolder instanceof LabelViewHolder){
            LabelViewHolder holder = (LabelViewHolder) viewHolder;
            if(current_position>=0){
                if(position==current_position){

                    holder.mLabelData.cleanTag();
                    ArrayList<TextView> tvList = holder.mLabelData.setDataDefaultSelect(mLabelDatas.get(position).getLabelList());

                    for(int i = 0;i<tvList.size();i++){
                        TextView textView = tvList.get(i);
                        if(!mSelectView.contains(textView)){
                            mSelectView.add(textView);


                            if(!selectStr.contains(mLabelDatas.get(position).getLabelList().get(i).getName())){
                                selectStr.add(mLabelDatas.get(position).getLabelList().get(i).getName());
                                mSelectBean.add(mLabelDatas.get(position).getLabelList().get(i));
                                mSelectDatas.get(position).getLabelList().add(mLabelDatas.get(position).getLabelList().get(i));
                            }
//                            if(!mSelectDatas.get(position).getLabelList().contains(mLabelDatas.get(position).getLabelList().get(i))){
//                                mSelectDatas.get(position).getLabelList().add(mLabelDatas.get(position).getLabelList().get(i));
//                            }
                        }else{
//                        if(!mSelectBean.contains(mLabelDatas.get(current_position).getLabelList().get(i))){
//                            mSelectBean.add(mLabelDatas.get(current_position).getLabelList().get(i));
//                        }

                        }
                    }
                }else{
                    holder.mLabelData.setData(mLabelDatas.get(position).getLabelList());
                }
            }else{
                holder.mLabelData.cleanTag();
                holder.mLabelData.setData(mLabelDatas.get(position).getLabelList());
            }

            holder.mLabelType.setText(mLabelDatas.get(position).getType());

            final FlowLayout flowLayout = holder.mLabelData;
            flowLayout.setOnTagClickListener(new FlowLayout.OnTagClickListener() {
                @Override
                public void TagClick(InterestLabelBean.ListBean.LabelListBean bean, TextView textView) {
//                mSelectBean.add(bean);
                    if(position!=0){
                        if(selectStr.contains(bean.getName())){
                            selectStr.remove(bean.getName());
                        }else{
                            selectStr.add(bean.getName());
                        }
                        if (mSelectBean.contains(bean)) {
                            mSelectBean.remove(bean);
                        }else{
                            mSelectBean.add(bean);
                        }
                        if(mSelectDatas.get(position).getLabelList().contains(bean)){
                            mSelectDatas.get(position).getLabelList().remove(bean);
                        }else{
                            mSelectDatas.get(position).getLabelList().add(bean);
                        }

//                selectStr.add(bean.getName());
                        textView.setTextColor(mContext.getResources().getColor(R.color.select_color));
                        textView.setBackgroundResource(R.drawable.corens_100_style_select);
                        Drawable dra = ContextCompat.getDrawable(mContext,R.drawable.select);
                        dra.setBounds(8,0,0,0);
                        textView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext,R.drawable.select),null,null,null);

                        if (!mSelectView.contains(textView)) {
                            mSelectView.add(textView);

                        } else {
                            textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
                            textView.setTextColor(Color.parseColor("#4a4a4a"));
                            textView.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                            mSelectView.remove(textView);
                        }
                        // 如果是认购起点
                    }else{
                        boolean flag_rgqd = false;
                        InterestLabelBean.ListBean.LabelListBean rgqd_bean= null;
                        for(int i= 0;i<allLabelDatas.get(0).getLabelList().size();i++){
                            // 如果有选中的
                            if(selectStr.contains(allLabelDatas.get(0).getLabelList().get(i).getName())){
                                flag_rgqd = true;
                                rgqd_bean = allLabelDatas.get(0).getLabelList().get(i);
                                break;
                            }
                        }
                        // 如果有选中的
                        if(flag_rgqd){
                            flowLayout.cleanTag();

                            selectStr.remove(rgqd_bean.getName());
                            mSelectBean.remove(rgqd_bean);
                            mSelectDatas.get(position).getLabelList().remove(rgqd_bean);
                            mSelectView1.clear();
                            if(bean!=rgqd_bean){
                                flowLayout.setData(mLabelDatas.get(position).getLabelList(),bean);
                                if(selectStr.contains(bean.getName())){
                                    selectStr.remove(bean.getName());
                                }else{
                                    selectStr.add(bean.getName());
                                }
                                if (mSelectBean.contains(bean)) {
                                    mSelectBean.remove(bean);
                                }else{
                                    mSelectBean.add(bean);
                                }
                                if(mSelectDatas.get(position).getLabelList().contains(bean)){
                                    mSelectDatas.get(position).getLabelList().remove(bean);
                                }else{
                                    mSelectDatas.get(position).getLabelList().add(bean);
                                }
                                mSelectView1.add(textView);
////                selectStr.add(bean.getName());
//                                textView.setTextColor(mContext.getResources().getColor(R.color.select_color));
//                                textView.setBackgroundResource(R.drawable.corens_100_style_select);
//                                Drawable dra = ContextCompat.getDrawable(mContext,R.drawable.select);
//                                dra.setBounds(8,0,0,0);
//                                textView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext,R.drawable.select),null,null,null);
//
//                                if (!mSelectView1.contains(textView)) {
//                                    mSelectView1.add(textView);
//
//                                } else {
//                                    textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
//                                    textView.setTextColor(Color.parseColor("#4a4a4a"));
//                                    textView.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
//                                    mSelectView1.remove(textView);
//                                }
                                // 选中上次选中的
                            }else{
                                flowLayout.setData(mLabelDatas.get(position).getLabelList());
                                textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
                                textView.setTextColor(Color.parseColor("#4a4a4a"));
                                textView.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                                mSelectView1.remove(textView);
                            }


                            // 没有选中的
                        }else{
                            if(selectStr.contains(bean.getName())){
                                selectStr.remove(bean.getName());
                            }else{
                                selectStr.add(bean.getName());
                            }
                            if (mSelectBean.contains(bean)) {
                                mSelectBean.remove(bean);
                            }else{
                                mSelectBean.add(bean);
                            }
                            if(mSelectDatas.get(position).getLabelList().contains(bean)){
                                mSelectDatas.get(position).getLabelList().remove(bean);
                            }else{
                                mSelectDatas.get(position).getLabelList().add(bean);
                            }

//                selectStr.add(bean.getName());
                            textView.setTextColor(mContext.getResources().getColor(R.color.select_color));
                            textView.setBackgroundResource(R.drawable.corens_100_style_select);
                            Drawable dra = ContextCompat.getDrawable(mContext,R.drawable.select);
                            dra.setBounds(8,0,0,0);
                            textView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext,R.drawable.select),null,null,null);

                            if (!mSelectView1.contains(textView)) {
                                mSelectView1.add(textView);

                            } else {
                                textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
                                textView.setTextColor(Color.parseColor("#4a4a4a"));
                                textView.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                                mSelectView1.remove(textView);
                            }
                        }
                    }

                }
            });
            if(mLabelDatas.get(position).getType().equals("地区")||mLabelDatas.get(position).getType().equals("发行机构")){
                holder.tv_more.setVisibility(View.VISIBLE);
            }else{
                holder.tv_more.setVisibility(View.GONE);
            }

            holder.tv_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();

                    bundle.putSerializable("datas",allLabelDatas.get(position));
                    bundle.putSerializable("citys",interestLabelBean);
                    bundle.putSerializable("jigou",jgBean);
                    bundle.putSerializable("select",mSelectBean);
                    mContext.startActivityForResult(new Intent(mContext, AllMechanismActivity.class).putExtras(bundle).putExtra("position",position)
                            ,0);
                }
            });
        }else{
            if(viewHolder instanceof FootHolder) {
                FootHolder holder = (FootHolder) viewHolder;
                final EditText editText_qtje = holder.et_qtje;
                final EditText editText_sy_start = holder.et_syqj_start;
                final EditText editText_sy_end = holder.et_syqj_end;
                editText_qtje.clearFocus();
                editText_sy_start.clearFocus();
                editText_sy_end.clearFocus();
                editText_qtje.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        qtje = editText_qtje.getText().toString();
                    }
                });

                editText_sy_start.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        syStart = editText_sy_start.getText().toString();
                        if(TextUtils.isEmpty(syStart)){
                            syStart = "0";
                        }
                    }
                });

                editText_sy_end.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        syEnd = editText_sy_end.getText().toString();
                        if(TextUtils.isEmpty(syEnd)){
                            syEnd = "0";
                        }
                    }
                });

            }
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_FOOT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_interest_label_new_two, parent, false);
            return new FootHolder(view);
        } else { //item_ranking_new
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_interest_label_new, parent, false);
            return new LabelViewHolder(view);
        }
//        item_interest_label
//        LabelViewHolder holder = new LabelViewHolder(LayoutInflater.from(
//                mContext).inflate(R.layout.item_interest_label_new, parent,
//                false));
//        return holder;
    }


    @Override
    public int getItemCount() {
        return mLabelDatas == null ? 0 : mLabelDatas.size();
    }

    class LabelViewHolder extends RecyclerView.ViewHolder {

        TextView mLabelType,tv_more;
        FlowLayout mLabelData;

        public LabelViewHolder(View view) {
            super(view);
            mLabelType = (TextView) view.findViewById(R.id.tv_label_title);
            mLabelData = (FlowLayout) view.findViewById(R.id.fl_label_data);
            tv_more = (TextView) view.findViewById(R.id.tv_more);
        }
    }

    class FootHolder extends RecyclerView.ViewHolder {
        EditText et_qtje,et_syqj_start,et_syqj_end;
        public FootHolder(View view) {
            super(view);
            et_qtje = (EditText) view.findViewById(R.id.et_qtje);
            et_syqj_start = (EditText) view.findViewById(R.id.et_syqj_start);
            et_syqj_end = (EditText) view.findViewById(R.id.et_syqj_end);
        }
    }
}
